package service;

import bean.CustomBean;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class ForwardServlet extends HttpServlet {
    private static final long serialVersionUID = -568949620995390064L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext app = req.getServletContext();
        int var1 = Integer.parseInt(req.getParameter("f1"));
        String var2 = req.getParameter("f2");
        CustomBean bean = new CustomBean(var1, var2);
        req.setAttribute("object", bean);
        HttpSession sess = req.getSession();
        sess.setAttribute("bean", bean);
        String origin = req.getHeader("origin");
        String contextPath = req.getContextPath();
        String referer = req.getHeader("referer");
        if (referer.equals(origin + contextPath + "/el.html")) {
            RequestDispatcher rd = app.getRequestDispatcher("/WEB-INF/el.jsp");
            rd.forward(req, resp);
        } else if (referer.equals(origin + contextPath + "/jstl-core.html")) {
            RequestDispatcher rd = app.getRequestDispatcher("/WEB-INF/jstl-core.jsp");
            rd.forward(req, resp);
        } else {
            resp.sendRedirect("contents.html");
        }
    }
}
